package com.ss.web.tagmark.init;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.accept.ContentNegotiationStrategy;
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.springframework.web.servlet.view.xml.MarshallingView;

/**
 * 定制SpringMVC相关的配置，相当于之前配置springmvc-servlet.xml
 *
 * @author taofucheng
 *
 */
@Configuration
public class CustomMVCConfiguration implements WebMvcConfigurer, ApplicationContextAware {
	private ApplicationContext applicationContext;

	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		configurer.defaultContentType(MediaType.TEXT_HTML);
		configurer.mediaType("json", MediaType.APPLICATION_JSON_UTF8);
		configurer.mediaType("xml", MediaType.TEXT_XML);
		configurer.mediaType("txt", MediaType.TEXT_PLAIN);
		List<ContentNegotiationStrategy> strategies = new ArrayList<>();
		strategies.add(new PathExtensionContentNegotiationStrategy());
		//		 strategies.add(new FixedContentNegotiationStrategy(contentTypes));
		configurer.strategies(strategies);
		configurer.ignoreAcceptHeader(true);
	}

	public void configureViewResolvers(ViewResolverRegistry registry) {
		ContentNegotiatingViewResolver vr = applicationContext.getBean(ContentNegotiatingViewResolver.class);
		vr.setDefaultViews(Arrays.asList(jsonView(), xmlView(), txtView()));
	}

	private View jsonView() {
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		view.setExtractValueFromSingleKeyModel(false);//model中只有一个元素时，直接返回那个值，要关掉，否则后面再增加元素时，页面要随着更改
		return view;
	}

	private View xmlView() {
		MarshallingView view = new MarshallingView();
		view.setContentType(MediaType.TEXT_XML_VALUE + ";charset=UTF-8");
		view.setMarshaller(new XStreamMarshaller());
		return view;
	}

	private View txtView() {
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		view.setContentType(MediaType.TEXT_PLAIN_VALUE + ";charset=UTF-8");
		view.setExtractValueFromSingleKeyModel(false);
		return view;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
}